package com.whitedust.top30;

import com.whitedust.structure.ListNode;
import com.whitedust.structure.TreeNode;

import java.util.List;

/**
 * @author A
 * @since 2025/11/3 19:15
 */

public class Top25 {
    public void reorderList(ListNode head) {
        ListNode midNode = getMidNode(head);
        ListNode head2 = reverseListNode(midNode);
        while (head2.next != null) {
            ListNode next1 = head.next;
            ListNode next2 = head2.next;
            head.next = head2;
            head2.next = next1;
            head = next1;
            head2 = next2;
        }
    }

    //如何找到链表的中间节点
    private ListNode getMidNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    private ListNode reverseListNode(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}
